//
//  LLMallViewController.m
//  LoveLove
//
//  Created by wkf on 2019/3/27.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMallViewController.h"
#import "LLMallTableViewController.h"
#import "LLSideModel.h"
#import "LLShoppingCartViewController.h"

@interface LLMallViewController ()<XYSearchViewDelegate>

/**
 一级目录
 */
@property (strong, nonatomic) NSMutableArray *titleArr;
//@property (copy, nonatomic) NSString *<#name#>;

/**
 二级目录
 */
@property (strong, nonatomic) NSMutableArray *secondTitleArr;

@property (strong, nonatomic) LLSideView *sideView;
@property (strong, nonatomic) NSArray *sideModels;
@end

@implementation LLMallViewController


- (instancetype)init {
    self = [super init];
    if (self) {
        [self initVCSetting];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationBar];
    [self getFenleiData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.tintColor = UIColor.whiteColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:LLRegularFont(18)}];
    self.navigationController.navigationBar.barTintColor = LLColor(218, 13, 13);
}

- (void)initNavigationBar {
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reside"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoppingcCar_white"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClick)];
    
    XYSearchView *searchView = [[XYSearchView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth-70, 30) isTap:YES];
    searchView.placeholder = @"请输入搜索内容";
    searchView.delegate = self;
    self.navigationItem.titleView = searchView;
}

- (void)initVCSetting {
    self.selectIndex = 0;
    self.menuViewStyle = WMMenuViewStyleLine;
    self.titleColorNormal = LLColorRGB(67);
    self.titleSizeNormal = 16;
    self.titleSizeSelected = 16;
    self.titleColorSelected = UIColor.blackColor;
    self.automaticallyCalculatesItemWidths = YES;
    self.progressViewIsNaughty = YES;
    self.progressWidth = 15;
    self.progressHeight = 3;
    self.progressColor = LLColor(225, 0, 0);
    self.progressViewCornerRadius = 1.5;
}

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titleArr.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titleArr[index][@"classify_name"];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    LLMallTableViewController *vc = LLMallTableViewController.new;
    vc.index = index;
    if (self.titleArr.count != 0) {
         vc.dataDic = self.titleArr[index];
    }
   
    return vc;
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    CGFloat width = [super menuView:menu widthForItemAtIndex:index];
    return width + 20;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    //    CGFloat leftMargin = self.showOnNavigationBar ? 50 : 0;
    //    CGFloat originY = self.showOnNavigationBar ? 0 : CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    return CGRectMake(0, 0, self.view.frame.size.width, 50);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, self.view.frame.size.width, self.view.frame.size.height - originY);
}

#pragma mark - private method

- (void)leftBarButtonItemClick {
    [self.sideView show];
}

- (void)rightBarButtonItemClick {
    if (![LLUserManager isLogin]) return;
    [self.navigationController pushViewController:LLShoppingCartViewController.new animated:YES];
}

#pragma mark - lazy load
-(NSMutableArray *)secondTitleArr{
    if (!_secondTitleArr) {
        _secondTitleArr = [NSMutableArray array];
    }
    return _secondTitleArr;
}

- (NSMutableArray *)titleArr {
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}

- (LLSideView *)sideView {
    if (!_sideView) {
        _sideView = [[LLSideView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _sideView.models = self.sideModels;
        [self.tabBarController.view addSubview:_sideView];
    }
    return _sideView;
}

- (NSArray *)sideModels {
    if (!_sideModels) {
        NSMutableArray *mutableArr = [NSMutableArray array];
        for (NSString *title in @[@"幼儿园",@"小学",@"初中",@"高中",@"大学"]) {
            LLSideModel *model = LLSideModel.new;
            model.title = title;
            model.isSelected = @"0";
            [mutableArr addObject:model];
        }
        _sideModels = mutableArr;
    }
    return _sideModels;
}

#pragma mark---获取分类
-(void)getFenleiData{
    NSDictionary *dic = @{@"mall_id":@"1"};
    [self showSVProgressHUD];
    [LLHttpTool post:MALLHOST headParams:nil params:dic success:^(NSDictionary *result) {
       
        NSArray *restltArr = result[@"data"];
        for (NSDictionary *dic1 in restltArr) {
            if ([dic1[@"classify_id"] integerValue] == 0) {
                [self.titleArr addObject:dic1];
            }else{
                [self.secondTitleArr addObject:dic1];
            }
        }
       
        NSDictionary *dic2 = @{@"classify_name":@"推荐",@"classify_id":@"0",@"id":@"10000"};
        [self.titleArr insertObject:dic2 atIndex:0];
//        NSDictionary *dic2 = @{@"classify_name":@"推荐",@"classify_id":@"0",@"id":@"10000"};
//        [self.titleArr addObject:dic2];
        [self reloadData];

    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}
@end
